package com.qf.java2201.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @date 2022/3/29
 * @desc    解决  乱码问题的过滤器
 */
@WebFilter(urlPatterns = {"/*"})
public class CharacterFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        String requestURI = req.getRequestURI();

        if(requestURI.contains(".css")||requestURI.contains(".js") ){
            //一定要注意   像 css 和  js 这些静态资源   不要 响应成  text/html
            chain.doFilter(request,response);
        }else{

            request.setCharacterEncoding("utf-8");
            // 设置 浏览器 解析页面的类型
            response.setContentType("text/html; charset=UTF-8");

            chain.doFilter(request,response);
        }


    }

    @Override
    public void destroy() {

    }
}
